package com.zp.drug.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.common.core.constant.CacheNames;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import com.zp.common.redis.utils.CacheUtils;
import com.zp.drug.api.bo.DrugStorageDeptBo;
import com.zp.drug.api.vo.DrugStorageDeptVo;
import com.zp.drug.entity.DrugStorageDept;
import com.zp.drug.mapper.DrugStorageDeptMapper;
import com.zp.drug.service.IDrugStorageDeptService;
import com.zp.drug.service.IStorageVsDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 药品库存单位字典Service业务层处理
 *
 * @author zhang peng
 * @ date 2023-06-18
 */
@RequiredArgsConstructor
@Service
public class DrugStorageDeptServiceImpl implements IDrugStorageDeptService {

    private final DrugStorageDeptMapper baseMapper;

    private final IStorageVsDeptService storageVsDeptService;

    /**
     * 查询药品库存单位字典
     */
    @Override
    public DrugStorageDeptVo queryById (String id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询药品库存单位字典列表(分页)
     */
    @Override
    public TableDataInfo<DrugStorageDeptVo> queryPageList (DrugStorageDeptBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DrugStorageDept> lqw = buildQueryWrapper(bo);
        Page<DrugStorageDeptVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询药品库存单位字典列表(不分页)
     */
    @Override
    public List<DrugStorageDeptVo> queryList (DrugStorageDeptBo bo) {
        LambdaQueryWrapper<DrugStorageDept> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DrugStorageDept> buildQueryWrapper (DrugStorageDeptBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DrugStorageDept> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStorageCode()), DrugStorageDept::getStorageCode, bo.getStorageCode());
        lqw.like(StringUtils.isNotBlank(bo.getStorageName()), DrugStorageDept::getStorageName, bo.getStorageName());
        lqw.eq(StringUtils.isNotBlank(bo.getStorageType()), DrugStorageDept::getStorageType, bo.getStorageType());
        lqw.like(StringUtils.isNotBlank(bo.getDrugClass()), DrugStorageDept::getDrugClass, bo.getDrugClass());
        lqw.eq(StringUtils.isNotBlank(bo.getDisburseNoPrefix()), DrugStorageDept::getDisburseNoPrefix, bo.getDisburseNoPrefix());
        lqw.eq(StringUtils.isNotBlank(bo.getDisburseNoAva()), DrugStorageDept::getDisburseNoAva, bo.getDisburseNoAva());
        lqw.eq(StringUtils.isNotBlank(bo.getExportNoAva()), DrugStorageDept::getExportNoAva, bo.getExportNoAva());
        lqw.eq(StringUtils.isNotBlank(bo.getExportNoPrefix()), DrugStorageDept::getExportNoPrefix, bo.getExportNoPrefix());
        lqw.eq(StringUtils.isNotBlank(bo.getImportNoAva()), DrugStorageDept::getImportNoAva, bo.getImportNoAva());
        lqw.eq(StringUtils.isNotBlank(bo.getImportNoPrefix()), DrugStorageDept::getImportNoPrefix, bo.getImportNoPrefix());
        lqw.eq(StringUtils.isNotBlank(bo.getYgFlag()), DrugStorageDept::getYgFlag, bo.getYgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getWardCode()), DrugStorageDept::getWardCode, bo.getWardCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), DrugStorageDept::getOrgId, bo.getOrgId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugGrantType()), DrugStorageDept::getDrugGrantType, bo.getDrugGrantType());
        lqw.eq(StringUtils.isNotBlank(bo.getClinicOrVisit()), DrugStorageDept::getClinicOrVisit, bo.getClinicOrVisit());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), DrugStorageDept::getRemarks, bo.getRemarks());
        lqw.orderByAsc(DrugStorageDept::getSort);
        return lqw;
    }

    /**
     * 新增药品库存单位字典
     */
    @Override
    public Boolean insertByBo (DrugStorageDeptBo bo) {
        DrugStorageDept add = BeanUtil.toBean(bo, DrugStorageDept.class);
        validEntityBeforeSave(add);
        add.initEntity();
        add.setId(add.getStorageCode());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        CacheUtils.put(CacheNames.DRUG_STORAGE_DEPT, add.getId(), add);
        return flag;
    }

    /**
     * 修改药品库存单位字典
     */
    @Override
    public Boolean updateByBo (DrugStorageDeptBo bo) {
        DrugStorageDept update = BeanUtil.toBean(bo, DrugStorageDept.class);
        update.updateInfo();
        validEntityBeforeSave(update);
        CacheUtils.evict(CacheNames.DRUG_STORAGE_DEPT, update.getId());
        CacheUtils.put(CacheNames.DRUG_STORAGE_DEPT, update.getId(), update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave (DrugStorageDept entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除药品库存单位字典
     */
    @Override
    public Boolean deleteWithValidByIds (Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean b = baseMapper.deleteBatchIds(ids) > 0;
        ids.forEach(item -> {
            CacheUtils.evict(CacheNames.DRUG_STORAGE_DEPT, item);
        });
        return b;
    }

    /**
     * 刷新缓存（药品库存单位字典）
     */
    @Override
    public void refreshCacheDrugStorageDept () {
        List<DrugStorageDept> list = baseMapper.selectList();
        list.forEach(item -> {
            CacheUtils.put(CacheNames.DRUG_STORAGE_DEPT, item.getId(), item);
        });
    }

    /**
     * 获取药品库存单位字典信息list
     *
     * @param bo
     * @return
     */
    @Override
    public List<DrugStorageDeptVo> getDrugStorageDeptVoList (DrugStorageDeptBo bo) {
        LambdaQueryWrapper<DrugStorageDept> lqw = buildQueryWrapper(bo);
        List<DrugStorageDeptVo> drugStorageDeptVos = baseMapper.selectVoList(lqw);

        return drugStorageDeptVos;
    }

    /**
     * 以科室id查询对应的药房、药库信息
     *
     * @param bo
     * @return
     */
    @Override
    public List<DrugStorageDeptVo> getDrugVsDeptList (DrugStorageDeptBo bo) {
        return baseMapper.getDrugVsDeptList(bo);
    }
}
